package com.by.basicweb.jsonserializer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.by.toolkit.consts.DatePatternConst;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;

/**
 * 反序列化LocalDateTime
 *
 * @author YanBingHao
 * @since 2023/12/29
 */
public class LocalDateJsonToLongMinDeserializer extends JsonDeserializer<Long> {

    @Override
    public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {

        if (Objects.nonNull(p) && StringUtils.isNotBlank(p.getText())) {
            LocalDate localDate = LocalDateTimeUtil.parseDate(p.getText(), DatePatternConst.DATE_PATTERN);
            LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
            return LocalDateTimeUtil.toEpochMilli(localDateTime);
        }

        return null;
    }

}
